package com.suming.springboot_game.controller;

import com.suming.springboot_game.common.Result;
import com.suming.springboot_game.entity.Role;
import com.suming.springboot_game.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 招募角色
     * @param role
     * @return
     */
    @PostMapping("/recruit")
    public Result recruit(@RequestBody Role role){
        return Result.success(roleService.recruit(role));
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        return Result.success(roleService.selectAll());
    }

    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize,
                       @RequestParam(defaultValue = "") String name){
        pageNum = (pageNum - 1) * pageSize;
        Map<String,Object> res = new HashMap<>();
        List<Role> data = roleService.selectPage(pageNum,pageSize,name);
        Integer total = roleService.countByName(name);
        res.put("data",data);
        res.put("total",total);
        return Result.success(res);
    }

    @PostMapping("/dismiss")
    public Result dismiss(@RequestBody List<Integer> ids){
        return Result.success(roleService.dismiss(ids));
    }

}
